<?php

if (!defined('DIR_SEP')) {
    define('DIR_SEP', DIRECTORY_SEPARATOR);
}
if (!defined('MODULES_DIR')) {
    define('MODULES_DIR', dirname(__FILE__).DIR_SEP."..".DIR_SEP);
}
require_once(MODULES_DIR.'db.inc');
require_once(MODULES_DIR.'config.inc');

class BlogComment
{
	public $id;
	public $postId;
	public $content;
	public $entryDate;
	public $email;
	
	public static function add($postId,$content,$email)
	{
		try
		{
			$content = str_replace("'","\'",$content);
				
			$sql ="insert into t_blog_post_comment (post_id,content,email)
					values (".$postId.",'".$content."','".$email."')";

			$query = mysql_query($sql,DB::getInstance()->getDB());
			
			if (!$query)
				throw new Exception(mysql_error());
			
			return true;
		}
		catch(Exception $ex)
		{
			return false;
		}		
		
	}
	
	public static function getByPost($postId)
	{
		try
		{
			$sql ="select tbpc.id,tbpc.post_id,tbpc.entry_date,tbpc.content,tbpc.email
					FROM t_blog_post_comment tbpc 
					where tbpc.post_id = ".$postId;

			$query = mysql_query($sql,DB::getInstance()->getDB());

			if (!$query)
				throw new Exception(mysql_error());
			
			$result = array();
			
			while( list($id,$post_id,$entry_date,$content,$email) = mysql_fetch_row($query))
			{
				$comment = new BlogComment();
				
				$comment->id = $id;
				$comment->postId = $feed_id;
				$comment->entryDate = strtotime($entry_date);
				$comment->content = $content;
				$comment->email = $email;

				$result[] = $comment;
			}

			return $result;
		}
		catch(Exception $ex)
		{
			die("BlogComment->getByPost:".$ex->getMessage());
			return null;
		}
	}
	
	public static function delete($commentId)
	{
		$query = mysql_query("delete from t_blog_post_comment where id =".$commentId
							,DB::getInstance()->getDB());
		if(!$query)
			return false;
			
		return true;
	}	

}

?>